package com.briup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.comon.R;
import com.briup.entity.Employee;
import com.briup.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import sun.net.httpserver.HttpsServerImpl;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Auther: liao
 * @Date: 2022/9/11-09-11-11:17
 * @Description：com.briup.controller
 */
@RestController
@Slf4j

@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.将页面传过来的密码进行md5加密
        String password = employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        
        //2.根据页面传过来的用户名去数据库查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        
        //这里是查询条件，这里是等值查询
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        
        //3.如果没有查询到则查询失败
        if (emp == null){
            return R.error("登录失败，没有该用户名！");
        }
        
        //4.密码比对，不一致登录失败
        if (!emp.getPassword().equals(password)){
            return R.error("登录失败!密码不正确");
        }
        //5.查看员工的状态，如果已禁用的状态，则返回禁用的信息
        if (emp.getStatus()==0){
            return R.error("账号已被禁用！");
        }
        
        //6.登录成功，存入session中
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    
    @PostMapping("/logout")
    public R<String> loginOut(HttpServletRequest request){
        //清理session保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    
    
    @PostMapping
    public R<String> save( HttpServletRequest request,@RequestBody Employee employee){

        log.info("新增员工，员工信息：{}",employee.toString());

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

        //获得当前登录用户的id
       // Long empId = (Long) request.getSession().getAttribute("employee");


        /*employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        
        //1.先构造分页的构造器
        Page pageInfo = new Page(page,pageSize);
        
        //2.再造我们的条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //StringUtils.isNotEmpty(name)这个话判断是否为空，如果为空，
        // 相当于没有这个查询条件，下面的语句相当于没有了，如果不为空，分页条件需要两个参数
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getCreateTime);
        
        //执行语句
        Page page1 = employeeService.page(pageInfo, queryWrapper);

        return R.success(page1);
        
        
    }


    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());

       /* long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);*/
       /* Long empId = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }


    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
    
    
    
    
    
    
    
    
}


